package com.liveneo.adapter.mobile.service.impl;

import java.util.Date;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.liveneo.adapter.base.entity.PrpmOcrDriverLicense;
import com.liveneo.adapter.base.mapper.PrpmOcrDriverLicenseMapper;
import com.liveneo.adapter.base.support.BaseServiceImpl;
import com.liveneo.adapter.mobile.model.request.OcrDriverLicenseRequest;
import com.liveneo.adapter.mobile.service.MobileOcrDrivelicenseService;
import com.liveneo.adapter.mobile.util.UUIDUtils;

/**
 * 领款人信息服务类
 * @author zhaoyy
 * @date 2016-09-28
 */
@Service
public class MobileOcrDrivelicenseServiceImpl extends BaseServiceImpl implements MobileOcrDrivelicenseService {
    @Autowired
    private PrpmOcrDriverLicenseMapper ocrDriverLicenseMapper;

    @Override
    public synchronized PrpmOcrDriverLicense getOcrDriverLicenseByCaseUuid(Map<String, Object> params) {
        return ocrDriverLicenseMapper.getOcrDriverLicenseByCaseUuid(params);
    }

    @Override
    public int updateOcrDriverLicense(PrpmOcrDriverLicense ocrDriverLicense, OcrDriverLicenseRequest request, int type) {
        if (type == 1) { // 更新领款人身份证信息
            ocrDriverLicense.setLicensePlate(request.getLicensePlate());
            ocrDriverLicense.setDrivingLicType(request.getDrivingLicType());
            ocrDriverLicense.setTestRecord(request.getTestRecord());
            ocrDriverLicense.setCarType(request.getCarType());
            ocrDriverLicense.setCarTypeCode(request.getCarTypeCode());
            ocrDriverLicense.setBandModel(request.getBandModel());
            ocrDriverLicense.setCarIdentification(request.getCarIdentification());
            ocrDriverLicense.setEngineCode(request.getEngineCode());
            if (StringUtils.isNotBlank(request.getRegistDate())) ocrDriverLicense.setRegistDate(request.getRegistDate());
            if (StringUtils.isNotBlank(request.getIssueDate())) ocrDriverLicense.setIssueDate(request.getIssueDate());
            if (StringUtils.isNotBlank(request.getDriver())) ocrDriverLicense.setDriver(request.getDriver());
            ocrDriverLicense.setModifyBy(request.getUserCode());
            ocrDriverLicense.setModifyTime(new Date());
        } else { // 更新领款人银行卡信息
            ocrDriverLicense.setDriverLicense(request.getDriverLicense());
            ocrDriverLicense.setPermDrive(request.getPermDrive());
            ocrDriverLicense.setPermDriveCode(request.getPermDriveCode());
            ocrDriverLicense.setFirstDate(request.getFirstDate());
            ocrDriverLicense.setEffectiveDate(request.getEffectiveDate());
            ocrDriverLicense.setValidity(request.getValidity());
            ocrDriverLicense.setFileNo(request.getFileNo());
            if (StringUtils.isNotBlank(request.getRegistDate())) ocrDriverLicense.setRegistDate(request.getRegistDate());
            if (StringUtils.isNotBlank(request.getIssueDate())) ocrDriverLicense.setIssueDate(request.getIssueDate());
            if (StringUtils.isNotBlank(request.getDriver())) ocrDriverLicense.setDriver(request.getDriver());
            ocrDriverLicense.setModifyBy(request.getUserCode());
            ocrDriverLicense.setModifyTime(new Date());
        }
        return ocrDriverLicenseMapper.updateByPrimaryKeySelective(ocrDriverLicense);
    }

    @Override
    public int saveOcrDriverLicense(OcrDriverLicenseRequest request, int type) {
        PrpmOcrDriverLicense ocrDriverLicense = new PrpmOcrDriverLicense();
        if (type == 1) { // 保存行驶证临牌信息
            ocrDriverLicense.setOcrDriverLicenseId(UUIDUtils.getUUIDStr());
            ocrDriverLicense.setSurveyUserCode(request.getUserCode());
            ocrDriverLicense.setCaseInfoId(request.getCaseUuid());
            ocrDriverLicense.setCaseNo(request.getCaseNo());
            ocrDriverLicense.setObjectName(request.getObjectName());
            ocrDriverLicense.setLossType(request.getLossType());
            ocrDriverLicense.setLossId(request.getLossId());
            ocrDriverLicense.setLossItemId(request.getLossUuid());
            ocrDriverLicense.setDriver(request.getDriver());
            ocrDriverLicense.setLicensePlate(request.getLicensePlate());
            ocrDriverLicense.setDrivingLicType(request.getDrivingLicType());
            ocrDriverLicense.setTestRecord(request.getTestRecord());
            ocrDriverLicense.setCarType(request.getCarType());
            ocrDriverLicense.setCarTypeCode(request.getCarTypeCode());
            ocrDriverLicense.setBandModel(request.getBandModel());
            ocrDriverLicense.setCarIdentification(request.getCarIdentification());
            ocrDriverLicense.setEngineCode(request.getEngineCode());
            ocrDriverLicense.setRegistDate(request.getRegistDate());
            ocrDriverLicense.setIssueDate(request.getIssueDate());
            ocrDriverLicense.setCreateTime(new Date());
            ocrDriverLicense.setCreateBy(request.getUserCode());
            ocrDriverLicense.setModifyBy(request.getUserCode());
            ocrDriverLicense.setModifyTime(new Date());
            if (StringUtils.isNotBlank(request.getDriver())) ocrDriverLicense.setDriver(request.getDriver());
        } else { // 保存驾驶证信息
            ocrDriverLicense.setOcrDriverLicenseId(UUIDUtils.getUUIDStr());
            ocrDriverLicense.setSurveyUserCode(request.getUserCode());
            ocrDriverLicense.setCaseInfoId(request.getCaseUuid());
            ocrDriverLicense.setCaseNo(request.getCaseNo());
            ocrDriverLicense.setObjectName(request.getObjectName());
            ocrDriverLicense.setLossType(request.getLossType());
            ocrDriverLicense.setLossId(request.getLossId());
            ocrDriverLicense.setLossItemId(request.getLossUuid());
            ocrDriverLicense.setDriverLicense(request.getDriverLicense());
            // TODO
            ocrDriverLicense.setPermDrive(request.getPermDrive());
            ocrDriverLicense.setPermDriveCode(request.getPermDriveCode());
            ocrDriverLicense.setFirstDate(request.getFirstDate());
            ocrDriverLicense.setEffectiveDate(request.getEffectiveDate());
            ocrDriverLicense.setValidity(request.getValidity());
            ocrDriverLicense.setFileNo(request.getFileNo());
            ocrDriverLicense.setIssueDate(request.getIssueDate());
            ocrDriverLicense.setRegistDate(request.getRegistDate());
            ocrDriverLicense.setCreateTime(new Date());
            if (StringUtils.isNotBlank(request.getDriver())) ocrDriverLicense.setDriver(request.getDriver());
            ocrDriverLicense.setCreateTime(new Date());
            ocrDriverLicense.setCreateBy(request.getUserCode());
            ocrDriverLicense.setModifyBy(request.getUserCode());
            ocrDriverLicense.setModifyTime(new Date());
        }
        return ocrDriverLicenseMapper.insertSelective(ocrDriverLicense);
    }

    @Override
    public PrpmOcrDriverLicense selectDrivelicenseByCaseNo(PrpmOcrDriverLicense params) {
        return ocrDriverLicenseMapper.selectDrivelicenseByCaseNo(params);
    }
}
